package com.fairytask.ui.item
{
	import com.fairytask.engine.components.BaseItem;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	import com.greensock.TimelineMax;
	import com.greensock.TweenAlign;
	import com.greensock.TweenMax;
	import com.greensock.easing.Linear;
	
	import flash.events.MouseEvent;
	
	/**
	 * 活跃度礼包 
	 * @author xujh
	 * 
	 */
	public class ActiveGiftItem extends BaseItem
	{
		//显示
		public var giftVg	:ViewGroup;	//礼品
		public var pickBtn	:Button;	//领取按钮
		//状态
		private var _canGet	:Boolean;	//可以领取
		private var _isGet	:Boolean;	//已经领取
		//数据
		private var _prizeID	:int;	//礼包id
		private var _needVal	:int;	//需要的活跃度，显示tips（备用）
		//按钮动画时间轴
		private var _timeLine	:TimelineMax;
		private var _isFlash	:Boolean;	//是否发光
		
		//GiftType类型
		private const SILVER	:int	=	1;
		private const SPIRIT	:int	=	2;
		private const GOLD		:int	=	3;
		private const ITEM		:int	=	4;
		
		public function ActiveGiftItem()
		{
			//初始化视图 
			giftVg	=	new ViewGroup(1,6,"CustSkin_017",56,56);
			addChild(giftVg);
			giftVg.addItemsEvent(MouseEvent.MOUSE_OVER,onOver);
			giftVg.addItemsEvent(MouseEvent.MOUSE_OUT,onOut);
			//领取按钮
			pickBtn	=	new Button("",46,31,"button_034");
			pickBtn.x	=	355;
			pickBtn.y	=	15;
			addChild(pickBtn);
			pickBtn.addEventListener(MouseEvent.CLICK,onPick);
			//初始化未领取
			this.canGet	=	false;
		}
		
		/**
		 * 领取奖励 
		 * @param evt
		 * 
		 */
		private function onPick(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_GIFT,_prizeID);
		}
		
		/**
		 * 显示物品tips 
		 * @param evt
		 * 
		 */
		private function onOver(evt:MouseEvent,pos:int):void
		{
			//没有物品
			if(null == giftVg.getItemByIndex(pos).getItem())
			{
				return;
			}
			//物品和银两
			if((giftVg.getItemByIndex(pos).getItem() as CardItem).isItem)
			{
				TipsManage.ShowItemTips((giftVg.getItemByIndex(pos).getItem() as CardItem).configID);
			}
			else
			{
				var type	:int = (giftVg.getItemByIndex(pos).getItem() as CardItem).moneyType;
				switch(type)
				{
					case CardItem.SILVER:
						TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000538",[(giftVg.getItemByIndex(pos).getItem() as CardItem).moneyVal]));
						break;
					case CardItem.SPIRIT:
						TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000540",[(giftVg.getItemByIndex(pos).getItem() as CardItem).moneyVal]));
						break;
					case CardItem.GOLD:
						TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000539",[(giftVg.getItemByIndex(pos).getItem() as CardItem).moneyVal]));
						break;
				}
			}
		}
		
		private function onOut(evt:MouseEvent,pos:int):void
		{
			ToolTips.hide();
		}
		
		/**
		 * 设置礼包奖励
		 * 
		 */
		private function setPrize():void
		{
			for(var i:int=0;i<6;i++)
			{
				var j	:int	=	i+1;
				//为空结束
				if(null == StaticIni.getIniVar(_prizeID,"GiftType"+j))
				{
					return;
				}
				var giftType	:int	=	int(StaticIni.getIniVar(_prizeID,"GiftType"+j));
				var type	:int;
				switch(giftType)
				{
					case SILVER:
						type	=	CardItem.SILVER;
						break;
					case SPIRIT:
						type	=	CardItem.SPIRIT;
						break;
					case GOLD:
						type	=	CardItem.GOLD;
						break;
					case ITEM:
						type	=	CardItem.ITEM;
						break;
				}
				var itemID	:int	=	int(StaticIni.getIniVar(_prizeID,"ItemId"+j));
				var amount	:int	=	int(StaticIni.getIniVar(_prizeID,"Amount"+j));
				var tmpItem	:CardItem	=	new CardItem();
				tmpItem.setData(type,itemID,amount);
				tmpItem.y	=	2;
				giftVg.getItemByIndex(i).clear();
				giftVg.getItemByIndex(i).addItem(tmpItem);
			}
			_needVal	=	int(StaticIni.getIniVar(_prizeID,"ActValue"));
		}
		
		/**
		 * 设置按钮状态 
		 * 
		 */
		private function setBtnStatus():void
		{
			//不可领取
			if(!_canGet)
			{
				pickBtn.label	=	StaticIni.getSwfLangStr("1000000654");
				giftVg.filters	=	null;
				pickBtn.enabled	=	false;
				_isFlash	=	false;
				playLine(_isFlash);
			}
			//已领取
			else if(_canGet && _isGet)
			{
				pickBtn.label	=	StaticIni.getSwfLangStr("1000000645");
				giftVg.filters	=	[Filter.GRAY];
				pickBtn.enabled	=	false;
				_isFlash	=	false;
				playLine(_isFlash);
			}
			else
			{
				pickBtn.label	=	StaticIni.getSwfLangStr("1000000654");
				giftVg.filters	=	null;
				pickBtn.enabled	=	true;
				//添加按钮发光效果
				_isFlash	=	true;
				playLine(_isFlash);
			}
		}
		
		/**
		 * 播放按钮动画 
		 * @param val
		 * 
		 */
		private function playLine(val:Boolean):void
		{
			if(val)
			{
				if(null == _timeLine)
				{
					//收藏动画效果初始化
					_timeLine = new TimelineMax({repeat:-1});
					_timeLine.appendMultiple([TweenMax.to(pickBtn, 0.2, {glowFilter:{color:0xFF0000, alpha:1, blurX:30, blurY:30,strength:1.5}, ease:Linear.easeNone}),
						TweenMax.to(pickBtn, 0.1, {glowFilter:{color:0xFF0000, alpha:1, blurX:0, blurY:0}, ease:Linear.easeNone})],0,TweenAlign.SEQUENCE);
//					_timeLine.stop();
				}
				else
				{
					_timeLine.play();
				}
			}
			else if(null != _timeLine)
			{
				_timeLine.stop();
				pickBtn.filters	=	null;
			}
		}
		
		/**
		 * 可以获取 
		 * @param val
		 * 
		 */
		public function set canGet(val:Boolean):void
		{
			_canGet	=	val;
			setBtnStatus();
		}
		
		public function get canGet():Boolean
		{
			return _canGet;
		}
		
		/**
		 * 已领取 
		 * @param val
		 * 
		 */
		public function set isGet(val:Boolean):void
		{
			_isGet	=	val;
			setBtnStatus();
		}
		
		public function get isGet():Boolean
		{
			return _isGet;
		}
		
		/**
		 * 是否发光 
		 * @return 
		 * 
		 */
		public function get isFlash():Boolean
		{
			return _isFlash;
		}
		
		/**
		 * 设置奖励礼包 
		 * @param val
		 * 
		 */
		public function set prizeID(val:int):void
		{
			_prizeID	=	val;
			setPrize();
		}
		
		public function get prizeID():int
		{
			return _prizeID;
		}
	}
}